package com.pt25.base.dep.kafka;

import com.pt25.base.constants.BaseConstants;
import com.pt25.base.util.JsonUtil;
import jakarta.annotation.Resource;
import lombok.Data;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

@Data
@Component
public class KafkaProducerService {


    @Resource(name = "kafkaTemplates")
    private Map<String, KafkaTemplate<String, String>> kafkaTemplates;

    public KafkaTemplate<String, String> getKafkaTemplate() {
        return kafkaTemplates.get(BaseConstants.DEFAULT);
    }

    public KafkaTemplate<String, String> getKafkaTemplate(String clusterName) {
        return kafkaTemplates.get(clusterName);
    }

    public <T> void sendMsg(String clusterName, String topic, T t) {
        KafkaTemplate<String, String> kafkaTemplate = getKafkaTemplate(clusterName);
        kafkaTemplate.send(topic, JsonUtil.toJson(t));
    }
}
